/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

/**
 *
 * @author Matt
 */
public class Util {

    static float PI = 3.14159265f;
    static float TWO_PI = 2*3.14159265f;

    static float strToFlt(String str) {
        return Float.intBitsToFloat(Integer.parseInt(str));
    }


    public static float abs(float x){
        if (x < 0) return -x;
        return x;

    }

    public static int buildProgram(int vertexShader, int fragmentShader) {

        IntBuffer pBuf = BufferUtils.createIntBuffer(1);

        int programID = GL20.glCreateProgram();

        GL20.glAttachShader(programID, vertexShader);
        GL20.glAttachShader(programID, fragmentShader);
        GL20.glLinkProgram(programID);
        GL20.glGetProgram(programID, GL20.GL_LINK_STATUS, pBuf);

        if (pBuf.get(0) == GL11.GL_FALSE) {
            System.out.println("Program shader didn't compile");
        }

        return programID;

    }

    public static int buildShader(String shaderName, int type) {
        IntBuffer pBuf = BufferUtils.createIntBuffer(1);
        int shaderID = GL20.glCreateShader(type);

        ByteBuffer source = getProgramCode(shaderName);

        shaderID = GL20.glCreateShader(type);
        GL20.glShaderSource(shaderID, source);
        GL20.glCompileShader(shaderID);
        GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS, pBuf);


        if (pBuf.get(0) == GL11.GL_FALSE) {
            System.out.println("Vertex shader didn't compile");
        }
        return shaderID;
    }

    private static ByteBuffer getProgramCode(String filename) {

        System.out.println("loading shader" + filename);
        ClassLoader fileLoader = FlightLifter.class.getClassLoader();
        InputStream fileInputStream = fileLoader.getResourceAsStream(filename);
        byte[] shaderCode = null;

        try {
            if (fileInputStream == null) {
                fileInputStream = new FileInputStream(filename);
            }
            DataInputStream dataStream = new DataInputStream(fileInputStream);
            dataStream.readFully(shaderCode = new byte[fileInputStream.available()]);
            fileInputStream.close();
            dataStream.close();
        } catch (Exception e) {
            System.out.println(e.getMessage() + ":" + filename);
        }
        ByteBuffer shaderPro = BufferUtils.createByteBuffer(shaderCode.length);


        shaderPro.put(shaderCode);
        shaderPro.flip();

        return shaderPro;
    }

    public static float min(float a, float b, float c) {

        if (a < b) {
            if (a < c) {
                return a;
            } else {
                return c;
            }
        }
        if (b < c) {
            return b;
        } else {
            return c;
        }


    }

    public static float max(float a, float b, float c) {

        if (a > b) {
            if (a > c) {
                return a;
            } else {
                return c;
            }
        }
        if (b > c) {
            return b;
        } else {
            return c;
        }


    }

    public static String readFile(String path) {
        ClassLoader fileLoader = FlightLifter.class.getClassLoader();
        InputStream fileInputStream = fileLoader.getResourceAsStream(path);
        byte[] shaderCode = null;
        try {
            if (fileInputStream == null) {
                fileInputStream = new FileInputStream(path);
            }
            DataInputStream dataStream = new DataInputStream(fileInputStream);
            dataStream.readFully(shaderCode = new byte[fileInputStream.available()]);
            fileInputStream.close();
            dataStream.close();
        } catch (Exception e) {
            System.out.println(e.getMessage() + ":" + path);
        }

        String temp = new String(shaderCode);
        return temp;
    }
}
